﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using AppEntity;

namespace AppEntity
{
    public partial class App
    {
        public class Entity
        {            
            internal XmlDocument AppDom;
            internal App App;
            public string AppID;
            public string ID;
            public string Name;
            public DateTime DateCreated
            {
                get { return this.AppDom.SelectSingleNode("/root/entities/entity[@name='" + this.ID + "']/@datecreated").Value.ToDateTime(); }                  
            }
            public string GetName(string Lang)
            {
                XmlNode Attr = this.AppDom.SelectSingleNode("/root/entities/entity[@name='" + this.ID + "']/@*[name()='" + Lang + "']");
                if (Attr == null) return string.Empty;
                return Attr.Value;
            }

            public Entity(string EntityID)
            {                
            }

            public XmlDocument GetCatDom()
            {
                string[] CatColumns = new string[] { "id", "name", "detail", "status", "private", "userid", "sort", "parentid", "datecreated" };

                System.Data.DataTable Dt = this.App.Sqlite.GetDataTable("SELECT "+CatColumns.Join(",")+" FROM [Entity-"+this.ID+"-Category]");
                StringBuilder sb = new StringBuilder();
                XmlWriterSettings Settings = new XmlWriterSettings();
                Settings.Encoding = Encoding.UTF8;
                XmlWriter Writer = XmlWriter.Create(sb, Settings);
                Writer.WriteStartElement("root");
                
                foreach (System.Data.DataRow Row in Dt.Rows)
                {
                    Writer.WriteStartElement("cat");
                    foreach(string Column in CatColumns)
                    {
                        Writer.WriteAttributeString(Column, Row[Column].ToString());
                    }
                    Writer.WriteEndElement();                    
                }
                Writer.WriteEndElement();
                Dt.Dispose();
                XmlDocument dom = new XmlDocument();
                dom.LoadXml(sb.ToString());
                dom.DocumentElement.AppendChild(dom.SelectSingleNode("/root/cat[@id='0']"));
                XmlNodeList Cats = dom.SelectNodes("/root/cat");
                foreach (XmlNode Cat in Cats)
                {
                    XmlNode ParNode = Cat.SelectSingleNode("@parentid");
                    if (ParNode == null) continue;
                    string Pid = ParNode.Value;
                    if (string.IsNullOrEmpty(Pid))
                    {
                        dom.DocumentElement.AppendChild(Cat);
                    }
                    else
                    {
                        dom.SelectSingleNode("//cat[@id='{0}']", Pid).AppendChild(Cat);
                    }
                }
                //TODO:实现分类排序，根据特殊字段 sort

                return dom;
            }

        }
    }
}
